package com.carpark.ejb.DAO;

import com.carpark.ejb.Model.ClientsModel;
import com.carpark.ejb.Model.ReceivablesModel;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateful;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 *
 * @author Krzysztof
 */
@Stateful
public class ReceivablesDAOImpl implements ReceivablesDAO {

    private EntityManager entityManager;

    public ReceivablesDAOImpl() {
        EntityManagerFactory factory = Persistence.createEntityManagerFactory("persistenceUnit");
        entityManager = factory.createEntityManager();
    }

    @Override
    public List<ReceivablesModel> getAllReceivable() {
        return (List<ReceivablesModel>) entityManager.createNamedQuery("ReceivablesModel.findAll").getResultList();
    }

    @Override
    public int getReceivableSize() {
        return entityManager.createNamedQuery("ReceivablesModel.findAll").getResultList().size();
    }

    @Override
    public ReceivablesModel getReceivablesById(Integer id) {
        return (ReceivablesModel) entityManager.createNamedQuery("ReceivablesModel.findByReceivableId").setParameter("receivableId", id).getSingleResult();
    }

    @Override
    public List<ReceivablesModel> getReceivablesByAmount(Double amount) {
        return (List<ReceivablesModel>) entityManager.createNamedQuery("ReceivablesModel.findByAmount").setParameter("amount", amount).getResultList();
    }

    @Override
    public List<ReceivablesModel> getReceivablesByCreationDate(Date creationDate) {
        return (List<ReceivablesModel>) entityManager.createNamedQuery("ReceivablesModel.findByCreationDate").setParameter("creationDate", creationDate).getResultList();
    }

    @Override
    public List<ReceivablesModel> getReceivablesByPayDeadline(Date payDeadline) {
        return (List<ReceivablesModel>) entityManager.createNamedQuery("ReceivablesModel.findByPayDeadline").setParameter("payDeadline", payDeadline).getResultList();
    }

    @Override
    public void addReceivable(ReceivablesModel receivable) {
        entityManager.getTransaction().begin();
        entityManager.persist(receivable);
        entityManager.flush();
        entityManager.refresh(receivable);
                entityManager.persist(receivable);
        entityManager.getTransaction().commit();
    }

    @Override
    public void updateReceivable(ReceivablesModel receivable) {
        ReceivablesModel rowToUpdate = getReceivablesById(receivable.getReceivableId());

              entityManager.getTransaction().begin();
        entityManager.persist(receivable);
        entityManager.flush();
        entityManager.getTransaction().commit();
    }

    @Override
    public void removeReceivable(Integer id) {
        ReceivablesModel rowToDelete = getReceivablesById(id);

        entityManager.getTransaction().begin();
        entityManager.remove(rowToDelete);
        entityManager.getTransaction().commit();

    }
    @Override
    public int getReceivablesByCreationDateSize(Date creationDate) {
    List<ReceivablesModel> list = (List<ReceivablesModel>) entityManager.createNamedQuery("ReceivablesModel.findByCreationDate").setParameter("creationDate", creationDate).getResultList();
    return list.size();
    }

    @Override
    public List<ReceivablesModel> getReceivablesByClient(ClientsModel cl) {
           return (List<ReceivablesModel>) entityManager.createNamedQuery("ReceivablesModel.findByClient").setParameter("clientId", cl).getResultList();  
    }

}